/**
 * Name: Avinash Sridhar	G Number: G00861300
 */
package tcp;

/**
 * @author asridhar
 *
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Client {

	public static void main(String[] args) throws IOException{
		try {

			InetAddress server_addr = InetAddress.getByName("localhost");

			//Prompt setup
			System.out.println("---------------------------------------");
			System.out.print("Enter search : ");

			Scanner userInput = new Scanner(System.in);
			while (userInput.hasNextLine())
			{
				//Prompt setup
				System.out.println("---------------------------------------");



				Socket client = new Socket(server_addr, Integer.parseInt(args[0]));
				System.out.println("TCP Client is now connected to TCP Server at client port: "+ Integer.parseInt(args[0]));

				BufferedReader ServerReader = new BufferedReader ( new InputStreamReader(client.getInputStream()) );
				PrintWriter ServerWriter = new PrintWriter(client.getOutputStream(), true);				

				String input = userInput.nextLine();
				if (input.equals("exit"))
				{
					System.out.println("TCP Client sending 'exit' status to TCP server...");
					ServerWriter.println(input);
					System.out.println("TCP Client exiting!");
					System.exit(0);
				}
				else
				{
					ServerWriter.println(input);
					System.out.println("TCP Client sent request to TCP Server...");

					String result = ServerReader.readLine();
					System.out.println("TCP Client has receieved the following result from TCP server: " + result);

					//Show all results
					System.out.println();
					System.out.println("******Search Results******");
					if (result.contains("%"))	//more than 1 result
					{
						List<String> results = Arrays.asList(result.split("%"));
						for (String eachResult : results)
						{
							String[] info = eachResult.split(" ");
							System.out.println("~~~~~");
							System.out.println("First Name: " + info[0]);
							System.out.println("Last Name: " + info[1]);
							System.out.println("Phone Number: " + info[2]);
							System.out.println("~~~~~");
						}
					}
					else
						System.out.println(result);	//"No Match." result
					System.out.println("**************************");
					System.out.println();
				}
				client.close();
				ServerReader.close();
				ServerWriter.close();

				//Prompt setup
				System.out.println("---------------------------------------");
				System.out.print("Enter search : ");
			}
			userInput.close();
		} catch (ConnectException e) {
			//e.printStackTrace();
			System.out.println("ERROR: Client has no server at " + Integer.parseInt(args[0]) + " to connect to! Client is Exiting!");
			System.exit(0);
		}
	}

}
